package com.blog.wu.myblog.service;

import com.blog.wu.myblog.base.BaseCrudService;
import com.blog.wu.myblog.dao.RoleMapper;
import com.blog.wu.myblog.entity.Role;
import com.blog.wu.myblog.entity.UserInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author wuyanshen
 * @Date 2018-10-12 下午5:06
 * @Discription 登录记录业务类
 */
@Service
public class RoleService extends BaseCrudService<RoleMapper, Role> {

    public List<Role> findRoles(UserInfo userInfo) {
        return super.dao.findRoles(userInfo);
    }


    @Transactional(readOnly = false)
    public boolean saveRole(Role role) {
        role.setRoleName(role.getRoleName());
        role.setCname(role.getCname());
        role.preInsert();
        int flag1 = super.dao.insert(role);
        int flag2 = super.dao.insertPermissionRole(role);
        if (flag1 > 0 && flag2 > 0) {
            logger.info("新增角色成功");
            return true;
        } else {
            logger.info("新增角色失败");
            return false;
        }
    }

    @Transactional(readOnly = false)
    public boolean deleteRole(Role role) {
        int flag1 = super.dao.deleteRole(role);
        int flag2 = super.dao.deleteRolePermission(role);
        if (flag1 > 0 && flag2 > 0) {
            logger.info("删除角色成功");
            return true;
        } else {
            logger.info("删除角色失败");
            return false;
        }
    }

    @Transactional(readOnly = false)
    public boolean updateRole(Role role) {
        int flag1 = super.dao.update(role);
        int flag2 = super.dao.deleteRolePermission(role);
        int flag3 = super.dao.insertPermissionRole(role);
        if (flag1 > 0 && flag3 > 0) {
            logger.info("更新角色成功");
            return true;
        } else {
            logger.info("更新角色失败");
            return false;
        }
    }

    public int countUserRole(Role role) {
        return super.dao.countUserRole(role);
    }
}
